package org.linlinjava.litemall.admin.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.linlinjava.litemall.admin.constant.LoginType;
import org.linlinjava.litemall.admin.util.BcryptUtil;
import org.linlinjava.litemall.db.domain.LitemallAdmin;

/**
 * @author wj
 * @date 2021/8/5 11:21
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {


    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        //获得前台传过来的密码
        ThirdPartyTypeToken admin=(ThirdPartyTypeToken)token;
        if(LoginType.WECHAT_LOGIN.equals(admin.getLoginType())){
            return true;
        }
        else{
            //这是数据库里查出来的密码
            String sqlOriginalPassword=((LitemallAdmin)info.getPrincipals().getPrimaryPrincipal()).getPassword();
            //进行比对
            return BcryptUtil.match(String.valueOf(admin.getPassword()),sqlOriginalPassword);
        }
    }
}
